package com.zg.generator.web;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.R;
import com.zg.common.core.dao.generator.GenTmpl;
import com.zg.common.core.dao.generator.GenTmplDao;
import com.zg.generator.vo.TmplUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zxb
 */
@RestController
@AllArgsConstructor
@Api(tags = {"模板管理"})
public class TmplController {
    private GenTmplDao tmplDao;

    @GetMapping("/tmpl/init")
    @ApiOperation("更新代码模板到数据库")
    public R<?> initTmpl2Db() {
        File tmplFolder = new File("E:\\workspace\\idea\\grid_back_new\\src\\main\\java\\com\\lt\\java\\generator\\ftl");
        File[] files = tmplFolder.listFiles();
        List<GenTmpl> insertList = new ArrayList<>();
        List<GenTmpl> updateList = new ArrayList<>();
        assert files != null;
        for (File file : files) {
            String code = file.getName();
            String content = FileUtil.readString(file, StandardCharsets.UTF_8);
            GenTmpl oldTmpl = tmplDao.getOne(
                    new LambdaQueryWrapper<GenTmpl>()
                            .eq(GenTmpl::getCode, code));
            if (oldTmpl != null) {
                oldTmpl.setContent(content);
                updateList.add(oldTmpl);
            } else {
                GenTmpl tmpl = new GenTmpl();
                tmpl.setCode(code);
                tmpl.setContent(content);
                insertList.add(tmpl);
            }
        }
        tmplDao.saveBatch(insertList);
        tmplDao.updateBatchById(updateList);
        return R.success();
    }

    @GetMapping("/tmpl/all")
    public R listAllTmpl(){
        List<GenTmpl> list = tmplDao.list(
                new LambdaQueryWrapper<GenTmpl>()
                        .orderByDesc(GenTmpl::getId)
        );
        return R.success(list);
    }

    @PostMapping("/tmpl/add")
    public R addTmpl(){
        GenTmpl obj = new GenTmpl();
        obj.setCode("新建模板");
        obj.setType("1");
        obj.setContent("");
        tmplDao.save(obj);
        return R.success(obj);
    }

    @PostMapping("/tmpl/del")
    public R delTmpl(@RequestBody List<Long> ids){
        tmplDao.removeBatchByIds(ids);
        return R.success();
    }

    @PostMapping("/tmpl/update")
    public R updateTmpl(@RequestBody TmplUpdateVo param){
        GenTmpl old = tmplDao.getById(param.getId());
        old.setCode(param.getCode());
        old.setType(param.getType());
        old.setContent(param.getContent());
        old.setSuffix(param.getSuffix());
        old.setSubPackage(param.getSubPackage());
        tmplDao.updateById(old);
        return R.success(old);
    }
}
